<?php
class IndexAction extends Action{
    public function index(){
    	if ($_SESSION['administer']['id'] && $_SESSION['administer']['name']){
    		import('@.ORG.Power');
    		$objpower = new Power();
    		$power = $objpower->index();
    		if ($_SESSION['administer']['id'] === "1"){
			    		
    			$powerAll = M("power")->order("power_order asc")->select();
    			foreach ($powerAll as $key=>$value){
    				$model_all = explode("/", $value['power_action']);
    				$powers[] = $value['power_action'];
    				foreach ($power as $k=>$v){
    					if ($k == $model_all['0']){
	    					if ( $model_all['1'] != "delNews" && $model_all['1'] != "editNews" && $model_all['1'] != "delActivity" && $model_all['1'] != "editActivity" ) {
								$models[$v][] = $value;
							}
    					}
    				}
    			}
    			$_SESSION['powerall'] = $powers;
    			$_SESSION['module'] = $models;
    		}else {
    			$powerAll = M("adminpower")->field("power.power_name,power.power_id,power.power_action")->join("admin ON admin.id = adminpower.admin_id")->join("power ON power.power_id = adminpower.power_id")->where("adminpower.admin_id=".$_SESSION['administer']['id'])->select();
    			foreach ($powerAll as $key=>$value){
    				$model_all = explode("/", $value['power_action']);
    				$powers[] = $value['power_action'];
    				foreach ($power as $k=>$v){
    					if ($k == $model_all['0']){
    						if ( $model_all['1'] != "delNews" && $model_all['1'] != "editNews" && $model_all['1'] != "delActivity" && $model_all['1'] != "editActivity" ) {
								$models[$v][] = $value;
							}
    					}
    				}
    			}
    			$_SESSION['powerall'] = $powers;
    			$_SESSION['module'] = $models;
    		}
    		$this->assign("left",$_SESSION['module']);
    		$this->assign("user",$_SESSION['administer']['name']);
    		//var_dump($_SESSION);die;
    		$this->display();
    	}else {
    		$this->login();
    	}
    }
    
    public function center(){
    	$this->display();
    }
    public function top(){
    	//var_dump($_SESSION['admin']);
    	$this->assign("user",$_SESSION['administer']['name']);
    	$this->display("top");
    }
    
    public function left(){
    	//var_dump($_SESSION['admin']);
    	$this->assign("left",$_SESSION['module']);
    	$this->display("left");
    }
    public function quit(){
    	//unset($_SESSION);
    	$_SESSION = array();
    	unset($_SESSION);
    	$this->assign("jumpUrl","__URL__/index");
    	$this->assign("waitSecond",3);
    	$this->success("退出成功");
    }
    public function verify(){
    	import("ORG.Util.Image");
    	Image::buildImageVerify();
    	//Image::GBVerify();
    }
    
    public function login(){
    	if ($this->isPost()){
	    	if (!addslashes(trim($_POST['user']))){
	    		$this->assign("jumpUrl","__URL__/index");
	    		$this->assign("waitSecond",3);
	    		$this->error("用户名不能为空");
	    	}
	    	if (!addslashes(trim($_POST['passwd']))){
	    		$this->assign("jumpUrl","__URL__/index");
	    		$this->assign("waitSecond",3);
	    		$this->error("密码不能为空");
	    	}
			if($_SESSION['verify'] != md5(trim($_POST['verify']))) {
				$this->assign("jumpUrl","__URL__/index");
				$this->assign("waitSecond",3);
				$this->error("验证码不正确！");
			}
			$pwd = md5(addslashes(trim($_POST['passwd'])));
			$user = M("admin")->where("admin_name='".addslashes(trim($_POST['user']))."' and admin_pwd='".$pwd."'")->find();

			if ($user){
				$_SESSION['administer']['id'] = $user['id'];
				$_SESSION['administer']['name'] = $user['admin_name'];
				$this->assign("jumpUrl","__URL__/index");
				$this->assign("waitSecond",3);
				$this->success("登录成功");
			}else {
				$this->assign("jumpUrl","__URL__/index");
				$this->assign("waitSecond",3);
				$this->error("用户名或密码不正确！");
			}
    	}else {
    		$this->display("login");
    	}
    }
}
?>